package com.rs.service.impl;

import com.rs.dao.RoleDao;
import com.rs.dao.StudentsDao;
import com.rs.entity.Student;
import com.rs.entity.excel.StudentExcelEntity;
import com.rs.service.StudentService;
import com.rs.util.DateUtil;
import com.rs.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentsDao studentsDao;



    @Override
    public List<Student> getStudentList(long page, long limit, String name){

        return studentsDao.getStudentList((page-1)*limit,limit,name);
    }

    @Override
    public Long getStudentSum(String name){

        return studentsDao.getStudentSum(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long addStudent(String name, String no,String phone,String email,String gender){
        //判断是否重复
        if(studentsDao.getNumByNo(0,no)>0){
            return -1;
        }
        return studentsDao.addStudent(name,no,phone,email,gender);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long updateStudent(Integer id,String name, String no,String phone,String email,String gender){
        //判断是否重复
        //判断是否重复
        if(studentsDao.getNumByNo(id,no)>0){
            return -1;
        }
        return studentsDao.updateStudent(id, name,no,phone,email,gender);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delStudent(long id){
        //删除角色
        studentsDao.deleteStudent(id);
    }

    @Override
    public Student getById(int id){
        return studentsDao.getById(id);
    }


    @Override
    public String uploadSave(List<StudentExcelEntity> list){

        List<Integer> ids=new ArrayList<>();
        // TODO 改为批量insert，效率 指数递增。
        for (StudentExcelEntity entity : list) {
            //判断学号是否存在
            int num=studentsDao.getNumByNo(0,entity.getStudentNo());
            Student student = new Student();
            student.setStudentNo(entity.getStudentNo());
            student.seteMail(entity.getEmail());
            student.setGender(entity.getGender());
            student.setName(entity.getName());
            student.setMoblephone(entity.getMobilePhone());
            if(num>0){
                //存入临时表
                student.setPassword("学生管理-学生导入-学生编号已存在");
                student.setDate(DateUtil.getDateFormatter());
                studentsDao.insertLs(student);
                ids.add(student.getId());
            }else{
                studentsDao.insert(student);
            }
        }
        String str="0";
        if(ids.size()>0){
            str="重复录入，已存入临时表student_temporary。id在"+ids.get(0)+"-"+ids.get(ids.size()-1)+"之间";
        }
        return str;
    }


    @Override
    public void uploadSaveByClass(List<StudentExcelEntity> list,int cid){
        for (StudentExcelEntity entity : list) {
            //判断学号是否存在
            int num=studentsDao.getNumByNo(0,entity.getStudentNo());
            //如果学生不存在，则新增
            if(num==0){
                Student student = new Student();
                student.setStudentNo(entity.getStudentNo());
                student.seteMail(entity.getEmail());
                student.setGender(entity.getGender());
                student.setName(entity.getName());
                student.setMoblephone(entity.getMobilePhone());
                studentsDao.insert(student);
                //存入临时表
                student.setPassword("课程管理-学生导入-学生不存在，在此处新增");
                student.setDate(DateUtil.getDateFormatter());
                studentsDao.insertLs(student);
            }
            //删除中间表
            studentsDao.delCs(cid,entity.getStudentNo());
            //学生分配课程
            studentsDao.insertCs(cid,entity.getStudentNo());
        }
    }



}
